import { db } from '@/storage'
import { Router } from 'vue-router'
import { configSetting } from '@/config'
import { start, checkAuth } from '@/utils'

export const useAuthToken = (router: Router) => {
  router.beforeEach((to, from, next) => {
    start()
    const token = db.get('token')
    const auth = to.meta?.notAuth
    const notToken = to.meta?.notToken
    const name = to.name as string
    // 需要权限 且需要 token
    if (!auth && checkAuth(name) && !notToken) {
      // 需要权限 且是权限 且 需要token的 ， --- > 判断有无 token
      if (!token) {
        return next({ name: configSetting.LoginMainLayoutName })
      }
      return next()
    }
    next()
  })
}
